package xushuo.dao.entity.weixin.managementUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xushuo.dao.entity.weixin.user.PublicUserInfo;
import xushuo.dao.service.token.AccessTokenService;
import xushuo.dao.service.tools.weixin.WeiXinUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import xushuo.service.defaultData.DefaultWeiXinData;

import java.io.IOException;

/**
 * Create by zhangpe0312@qq.com on 2018/2/1.
 */
@Service
public class ReqToPublicUserInfo {

    @Autowired
    WeiXinUtil weiXinUtil;

    @Autowired
    AccessTokenService accessTokenService;

    public static Logger logger = Logger.getLogger(ReqToPublicUserInfo.class);

    public PublicUserInfo getUserInfor(String openid) throws IOException {

        weiXinUtil.getAccessToken();

        String url = DefaultWeiXinData.weixinUser.
                getUserInforUrl.replace("ACCESS_TOKEN", accessTokenService.getAccessToken().getAccess_token()).
                replace("OPENID", openid);

        JSONObject jsonObject = weiXinUtil.doGetStr(url);

        if (jsonObject==null){
            logger.error("返回 "+ openid +" 的信息为空");
            return null;
        }

        PublicUserInfo user = null;
        try {
            String errcode = jsonObject.getString("errcode");
            String errmsg = jsonObject.getString("errmsg");
            logger.error("获取用户 openid: " + openid + " 时发生错误 errcode: " + errcode
                    + " errmsg: " + errmsg);
        }catch (Exception e){
            //14个属性

            System.out.println(jsonObject.getString("country"));

            user = new PublicUserInfo.Builder()

                    .setCity(jsonObject.getString("city"))
                    //国家
                    .setCountry(jsonObject.getString("country"))
                    //用户分组编号
                    .setGroupid(jsonObject.getString("groupid"))
                    //头像地址
                    .setHeadimgurl(jsonObject.getString("headimgurl"))
                    //语言
                    .setLanguage(jsonObject.getString("language"))
                    //性别
                    .setSex(Integer.parseInt(jsonObject.getString("sex")))
                    //用户微信唯一识别号
                    .setOpenid(jsonObject.getString("openid"))
                    //关注时间
                    .setSubscribe_time(jsonObject.getString("subscribe_time"))
                    //昵称
                    .setNickname(jsonObject.getString("nickname"))
//                    .setUnionid(jsonObject.getString("unionid"))
                    //
                    .setTagid_list(jsonObject.getString("tagid_list"))
                    //
                    .setSubscribe(Integer.parseInt(jsonObject.getString("subscribe")))
                    //
                    .setProvince(jsonObject.getString("province"))
                    //
                    .setRemark(jsonObject.getString("remark"))
                    .setGetTime()
                    .keep();
            logger.info("获取到了用户 " + user.toString());

        }
        return user;
    }
}
